package com.niannian.adapter;

import java.util.ArrayList;
import java.util.Date;

import com.niannian.R;
import com.niannian.activity.AddDateTipActivity;
import com.niannian.activity.DateTipActivity;
import com.niannian.activity.PushRegardsActivity;
import com.niannian.bean.RegardsTipBean;
import com.niannian.db.MyDBUser;
import com.niannian.dialog.SimpleListDialog;
import com.niannian.dialog.SimpleListDialog.onSimpleListItemClickListener;
import com.niannian.dialog.YesNoDialog2;
import com.niannian.dialog.YesNoDialog2.onSimpleOnCancelListener;
import com.niannian.dialog.YesNoDialog2.onSimpleOnClicklListener;
import com.niannian.util.Common;
import com.niannian.util.UserInfoManager;
import com.niannian.view.CircleImageView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 问候的提醒adapter
 * 
 * @author shenglin
 * 
 */
public class DateTipAdapter extends BaseAdapter {
	protected MyDBUser userInfoManager = UserInfoManager.getUserInfoInstance();
	private ArrayList<RegardsTipBean> fdatas;
	private LayoutInflater inflater;
	private Activity mActivity;

	private Handler handler;
	private String[] item = { "编辑", "删除" };

	public DateTipAdapter(Activity activity, Handler handler) {
		super();
		this.mActivity = activity;
		this.handler = handler;
		inflater = LayoutInflater.from(mActivity);
		this.fdatas = new ArrayList<RegardsTipBean>();
	}

	@Override
	public int getCount() {
		return fdatas.size();
	}

	@Override
	public Object getItem(int arg0) {
		return fdatas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	public ArrayList<RegardsTipBean> getFDatasList() {
		return fdatas;
	}

	@Override
	public View getView(final int position, View view, ViewGroup arg2) {
		ViewHolder holder = null;
		if (view == null) {
			view = inflater.inflate(R.layout.regards_tip_itme, null);
			holder = new ViewHolder();

			holder.ll_item_head = (LinearLayout) view.findViewById(R.id.ll_item_head);
			holder.tv_tip_title = (TextView) view.findViewById(R.id.tv_tip_title);
			holder.tv_tip_date = (TextView) view.findViewById(R.id.tv_tip_date);
			holder.tv_tip_date_num = (TextView) view.findViewById(R.id.tv_tip_date_num);

			holder.tv_tip_date_num_tip1 = (TextView) view.findViewById(R.id.tv_tip_date_num_tip1);
			holder.tv_tip_date_num_tip2 = (TextView) view.findViewById(R.id.tv_tip_date_num_tip2);

			holder.bt_tip_send_greeting = (Button) view.findViewById(R.id.bt_tip_send_greeting);

			holder.riv_is_triggered_tip = (CircleImageView) view.findViewById(R.id.riv_is_triggered_tip);

			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		final RegardsTipBean fdata = fdatas.get(position);
		holder.tv_tip_title.setText(fdata.getTitle());

		String Specdate = Common.sgmdateMD(fdata.getSpec_date());
		holder.tv_tip_date.setText(Specdate.split("/")[0] + "月" + Specdate.split("/")[1] + "日");

		int time = (Integer) fdata.getSpec_date() - Integer.parseInt(Common.getChangeTime(Common.sgmdate3((int) ((new Date()).getTime() / 1000))));
		int timeday = (time / 86400);
		holder.tv_tip_date_num_tip1.setVisibility(View.GONE);
		holder.tv_tip_date_num_tip2.setVisibility(View.VISIBLE);

		holder.tv_tip_date_num.setTextColor(mActivity.getResources().getColor(R.color.col_f));
		if (timeday <= 0) {
			holder.tv_tip_date_num.setText("今");
		} else if (timeday == 1) {
			holder.tv_tip_date_num.setText("明");
		} else if (timeday == 2) {
			holder.tv_tip_date_num.setText("后");
		} else {
			holder.tv_tip_date_num.setText(timeday + "");
			holder.tv_tip_date_num_tip1.setVisibility(View.VISIBLE);
			holder.tv_tip_date_num.setTextColor(mActivity.getResources().getColor(R.color.col_e));
		}

		if (fdata.getIs_triggered() == 1 && "false".equals(fdata.getIs_checked())) {
			holder.riv_is_triggered_tip.setVisibility(View.VISIBLE);
		} else {
			holder.riv_is_triggered_tip.setVisibility(View.GONE);
		}

		holder.bt_tip_send_greeting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mActivity, PushRegardsActivity.class);
				intent.putExtra("from", PushRegardsActivity.FROM_PUSH_REGARDS);
				mActivity.startActivity(intent);
				mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			}
		});
		holder.ll_item_head.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				initChooseDialog(fdata, position);
				return false;
			}
		});

		holder.ll_item_head.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bundle pBundle = new Bundle();
				pBundle.putSerializable("fdata", fdata);
				pBundle.putInt("from", 2);
				Intent data = new Intent(mActivity, AddDateTipActivity.class);
				data.putExtras(pBundle);
				mActivity.startActivityForResult(data, DateTipActivity.EDIT_TIP);
				Message message = new Message();
				message.arg1 = fdata.getId();
				message.what = DateTipActivity.TIP_CHECK;
				handler.sendMessage(message);

			}
		});

		if (fdata.getIsdelete() == 1) {
			removeListItem(view, position);
		}

		return view;
	}

	SimpleListDialog listDialog;

	private void initChooseDialog(final RegardsTipBean fdata, final int listPostion) {
		listDialog = new SimpleListDialog(mActivity);
		listDialog.setAdapter(new ListDialogAdapter(mActivity, item));

		listDialog.serOnSimpleListItemClikListener(new onSimpleListItemClickListener() {
			@Override
			public void onItemClick(int postion) {

				listDialog.dismiss();

				switch (postion) {
				case 0:
					Bundle pBundle = new Bundle();
					pBundle.putSerializable("fdata", fdata);
					pBundle.putInt("from", 1);
					Intent data = new Intent(mActivity, AddDateTipActivity.class);
					data.putExtras(pBundle);
					mActivity.startActivityForResult(data, DateTipActivity.EDIT_TIP);

					Message message = new Message();
					message.arg1 = fdata.getId();
					message.what = DateTipActivity.TIP_CHECK;
					handler.sendMessage(message);
					break;
				case 1:
					DeleteDialog(fdata.getId(), listPostion);
					break;

				}
			}
		});
		listDialog.show();
	}

	YesNoDialog2 tip;

	private void DeleteDialog(final int cid, final int position) {
		tip = new YesNoDialog2(mActivity, "确认要删除吗?");
		tip.setOnCancelListener(new onSimpleOnCancelListener() {

			@Override
			public void onCancel() {
				tip.dismiss();
			}
		});
		tip.setOnConfimClickListener(new onSimpleOnClicklListener() {

			@Override
			public void onClick() {
				Message msg = new Message();
				msg.what = DateTipActivity.DELETE_TIP;
				msg.arg1 = cid;
				handler.sendMessage(msg);
				tip.dismiss();
			}
		});
		tip.show();

	}

	protected void removeListItem(View rowView, final int positon) {
		final Animation animation = (Animation) AnimationUtils.loadAnimation(rowView.getContext(), R.anim.item_anim);
		animation.setAnimationListener(new AnimationListener() {
			public void onAnimationStart(Animation animation) {
			}

			public void onAnimationRepeat(Animation animation) {
			}

			public void onAnimationEnd(Animation animation) {
				fdatas.remove(positon);
				notifyDataSetChanged();
				animation.cancel();
			}
		});

		rowView.startAnimation(animation);
	}

	class ViewHolder {

		LinearLayout ll_item_head;
		TextView tv_tip_title;
		TextView tv_tip_date;
		TextView tv_tip_date_num;
		TextView tv_tip_date_num_tip1;
		TextView tv_tip_date_num_tip2;
		Button bt_tip_send_greeting;

		CircleImageView riv_is_triggered_tip;

	}

}
